package com.example.testapplication.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.testapplication.R;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.utils.StringUtils;

import java.io.IOException;
import java.util.HashMap;

public class UserRegisterActivity extends BaseActivity {
    private EditText userName;
    private EditText userPassword;
    private EditText userEmail;
    private Button btnUserLogin;

    @Override
    protected int initLayout() {
        return R.layout.activity_user_register;
    }

    @Override
    protected void initView() {
        userName = findViewById(R.id.user_register_name);
        userPassword = findViewById(R.id.user_register_password);
        userEmail = findViewById(R.id.user_register_email);
        btnUserLogin = findViewById(R.id.btn_user_register);
    }

    @Override
    protected void initData() {
        btnUserLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = userName.getText().toString().trim();
                String pwd = userPassword.getText().toString().trim();
                String email = userEmail.getText().toString().trim();
                register(account,pwd,email);
            }
        });
    }

    private void register(String account,String pwd,String email) {

        if (StringUtils.isEmpty(account)) {
            //Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            showToast("请输入账号");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            //Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            showToast("请输入密码");
            return;
        }
        if(StringUtils.isEmpty(email)){
            showToast("请输入邮箱");
            return;
        }

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("user", account);
        params.put("password", pwd);
        params.put("email",email);
        Api.config(ApiConfig.USER_REGISTER, params).postRequestLogin(new CallBack() {
            @Override
            public void onSuccess(String result) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast(result);
                    }
                });
            }

            @Override
            public void onFailure(IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast("no");
                    }
                });
            }
        });
    }
}